<?php
/*
 * @Author: your name
 * @Date: 2020-10-27 14:11:08
 * @LastEditTime: 2020-12-15 17:18:01
 * @LastEditors: Please set LastEditors
 * @Description: Netfilter模块关联
 * @FilePath: /special/application/adminapiv3/service/module_manage/NetfilterModuleRelationService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter;

use app\common\service\module_manage\netfilter\HostAccessLogConfig;
use app\common\service\module_manage\netfilter\HostLearnConfig;
use app\adminapiv3\service\module_manage\ModuleRelationService;
use app\common\definition\HostDefinition;
use app\common\definition\netfilter\LearnDataDefinition;
use app\common\exception\CustomException;
use think\Db;

class NetfilterModuleRelationService extends ModuleRelationService
{
    public $module = 'netfilter';
    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 16:27:38
     */
    public function add ()
    {
        $host_data = $this->getHost();
        $host_id_a = array_keys($host_data);
        //添加用户操作日志
        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '开启防火墙安全模块',
            'new_info' => '涉及主机主机:' . $host_name_s
        );
        //获取需要添加访问日志的主机
        $access_log_config = $this->getAddAccessLogConfigHost($host_id_a);
        //获取学习配置需要添加的主机
        $learn_config = $this->getAddLearnConfigHost($host_id_a);
        $linux_host_a = $windwos_host_a = [];
        foreach ($learn_config as $host_a)
        {
            if($host_data[$host_a['host_id']]['os'] == HostDefinition::LINUX_OPERATION_SYSTEM)
            {
                $linux_host_a[] = [
                    'host_id' => $host_a['host_id']
                ];
            }
            elseif ($host_data[$host_a['host_id']]['os'] == HostDefinition::WINDOWS_OPERATION_SYSTEM)
            {
                $windwos_host_a[] = [
                    'host_id' => $host_a['host_id']
                ];
            }
        }
        Db::startTrans();
        try{
            //添加netfilter配置
            Db::name('HostLearnConfig')->insertAll($learn_config);
            //获取linux防火墙默认配置
            Db::name('NetfilterFirewallConfigLinux')->insertAll($linux_host_a);
            //获取windwos防火墙默认配置
            Db::name('NetfilterFirewallConfigWindows')->insertAll($windwos_host_a);
            //添加防火墙访问 日志
            Db::name('HostAccessLogConfig')->insertAll($access_log_config);

            Db::commit();
        } catch (\think\Exception $e)
        {
            Db::rollback();
            throw new CustomException('添加模块失败:' . $e->getMessage(), 302);
        }
        
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-27 14:15:03
     */
    public function del ()
    {
        $host_id_a = $this->getHost('id');

        //添加用户操作日志
        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '禁用防火墙安全模块',
            'old_info' => '涉及主机主机:' . $host_name_s
        );
        
        //停止学习状态
        Db::name('HostLearnConfig')->whereIn('host_id', $host_id_a)->update([
            'etime' => time(),
            'learn_status' => LearnDataDefinition::STOP_STUDY,
        ]);
    }

    /**
     * @name: 冲突
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-28 14:17:59
     */
    public function conflictField (&$Host)
    {
        $Host->where('hgshield', '<>',1);
    }


    /**
     * @name: 冲突模块
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-28 15:23:03
     */
    public function conflictModule ()
    {
        return [
            'hgshield',
        ];
    }

    /**
     * @name: 获取需要添加访问日志的主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-10 10:53:19
     */
    private function getAddAccessLogConfigHost ($host_id_a)
    {
        $return = [];
        //主机日志配置
        $log_config_host = Db::name('HostAccessLogConfig')->whereIn('host_id', $host_id_a)->column('host_id');
        $insert_host_log_config = array_diff($host_id_a, $log_config_host);
        foreach ($insert_host_log_config as $host_id)
        {
            $return[] = [
                'host_id' => $host_id
            ];
        }

        return $return;
    }

    /**
     * @name: 获取需要添加学习日志的主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-10 10:55:47
     */
    private function getAddLearnConfigHost ($host_id_a)
    {
        $return = [];
        //主机学习配置
        $learn_config_host = Db::name('HostLearnConfig')->whereIn('host_id', $host_id_a)->column('host_id');
        $insert_host_learn_config = array_diff($host_id_a, $learn_config_host);
        foreach ($insert_host_learn_config as $host_id)
        {
            $return[] = [
                'host_id' => $host_id
            ];
        }

        return $return;
    }
}